﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebUI.BL.User;
using WebUI.DAL;
using WebUI.Helpers;
using WebUI.BL.ModelView;
namespace WebUI.AdminSite.User
{
    public partial class UserDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetRequestQueryString();
                tbl_User user = ModelBinding();
                if (user != null)
                {
                    ShowInformation(user);
                }
            }
            
        }

        private void ShowInformation(tbl_User user)
        {
            BindDropDownList();
            txtName.Text = user.Name;
            txtPwd.Text = user.Password;
            txtUserLoginName.Text = user.UserLoginName;
            hdfNo.Value = user.UserNo.ToString();
            var s=drpStatus.Items.FindByValue(user.Status);
            DisplayRoles(user);
        }

        private void BindDropDownList()
        {

        }

        private void GetRequestQueryString()
        {
            hdfId.Value = Request.QueryString["Id"] == null ? String.Empty : Request.QueryString["Id"]; //lay querystring
        }

        private string GetUrlPrevious()
        {
            Uri returnUrl = HttpContext.Current.Request.UrlReferrer;
            if (returnUrl != null)
                return returnUrl.ToString();
            return "#";

        }

        private tbl_User ModelBinding()
        {
            UserBL userBL = new UserBL();
            if (!String.IsNullOrEmpty(hdfId.Value))
                return userBL.GetUserById(int.Parse(hdfId.Value));
            return null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            UserBL userBL = new UserBL();
            tbl_User tblUser=userBL.GetUserById(int.Parse(hdfNo.Value));
            tblUser.Status="Deleted";
            userBL.Update(tblUser);
            Response.Redirect(GetUrlPrevious());
        }


        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            UserBL userBL = new UserBL();
            tbl_User tblUser = userBL.GetUserById(int.Parse(hdfNo.Value));
            tblUser.Status = drpStatus.SelectedValue;
            userBL.Update(tblUser);
            Response.Redirect(GetUrlPrevious());
        }

        private void DisplayRoles(tbl_User user)
        {
            UserBL userbl=new UserBL();
            var result = userbl.getRole(user.UserLoginName);
            foreach (GroupModelView m in result)
            {
                CheckBox chkbox = new CheckBox();
                chkbox.ID = "chk" + m.GroupName;
                chkbox.Text = m.GroupName;
                hldRoles.Controls.Add(chkbox);
            }
        }


       
    }
}